package com.jiuli_shoppingpro.controller;


import com.jiuli_shoppingpro.pojo.PageBean;
import com.jiuli_shoppingpro.service.ManagerService;
import com.jiuli_shoppingpro.utils.Result;
import com.jiuli_shoppingpro.utils.jwt.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;


@RestController
@Slf4j
@RequestMapping("/manager")
@CrossOrigin        /*解决跨域问题*/
public class ManagerController {

    @Autowired
    private ManagerService managerService;

    @GetMapping("/register")
    //注册
    public Result register(String managerName, String password) {
        String msg = managerService.register(managerName, password);
        if ("注册成功".equals(msg)) {
            return Result.success(msg);
        }
        return Result.error(msg);
    }

    @GetMapping("/login")
    //登录
    public Result login(@RequestParam String username, @RequestParam String password) {
        String msg = managerService.login(username, password);
        if ("该账号未注册，请先注册".equals(msg) || "您输入的密码不对，请重新输入".equals(msg)) {
            return Result.error(msg);
        }
        //设置token返回给前端
        HashMap<String, Object> claims = new HashMap<>();
        claims.put("managerName", msg);
        String token = JwtUtils.generateJwt(claims);
        return Result.success(msg);
    }

    @GetMapping("/selectConsumerByPagination")
    //条件分页查询消费者的数据
    public Result selectConsumerByPagination(@RequestParam(defaultValue = "1") int page,
                                             @RequestParam(defaultValue = "10") int pageSize,
                                             String mobile, String gender) {
        PageBean pageBean = managerService.selectConsumerByPagination(page, pageSize, mobile, gender);
        return Result.success(pageBean);
    }

    @GetMapping("/selectBusinessByPagination")
    //分页查询商家的信息
    public Result selectBusinessByPagination(@RequestParam(defaultValue = "1") int page,
                                             @RequestParam(defaultValue = "10") int pageSize) {
        PageBean pageBean = managerService.selectBusinessByPagination(page, pageSize);
        return Result.success(pageBean);
    }
}
